package com.hmkj.admin.controller.order;

import com.hmkj.admin.controller.BaseController;
import com.hmkj.common.utils.BigDecimalUtils;
import com.hmkj.core.po.operate.Agent;
import com.hmkj.core.po.order.OrderSimulate;
import com.hmkj.core.po.user.User;
import com.hmkj.core.service.operate.AgentService;
import com.hmkj.core.service.order.OrderSimulateService;
import com.hmkj.core.service.user.UserService;
import lombok.extern.log4j.Log4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import java.math.BigDecimal;

@Log4j
@Controller
@RequestMapping(BaseController.BASE_URI)
public class OrderSimulateController extends BaseController {

    @Resource
    OrderSimulateService orderSimulateService;
    @Resource
    UserService userService;
    @Resource
    AgentService agentService;

    /**
     * 策略列表
     * @return
     */
    @RequestMapping(value= "orderSimulate/list", method = RequestMethod.GET)
    public String list() {
        return ok("orderSimulate/list");
    }

    /**
     * 策略详情
     * @return
     */
    @RequestMapping(value= "orderSimulate/detail", method = RequestMethod.GET)
    public String detail(ModelMap modelMap, Integer orderSimulateId) {

        //策略详情
        OrderSimulate orderSimulate = orderSimulateService.selectByPrimaryKey(orderSimulateId);
        modelMap.put("orderSimulate",orderSimulate);

        //点买人
        User user = userService.selectByPrimaryKey(orderSimulate.getUserId());
        modelMap.put("user",user);

        //投资人
        User investor = userService.selectByPrimaryKey(orderSimulate.getInvestId());
        modelMap.put("investor",investor);

        //代理商
        Agent agent = agentService.selectByPrimaryKey(orderSimulate.getAgentId());
        modelMap.put("agent",agent);

        //点买人盈利分成
        BigDecimal userProfit = BigDecimal.ZERO;
        //投资人盈亏分配
        BigDecimal investorProfit = BigDecimal.ZERO;
        //点买人亏损金额
        BigDecimal userLose = BigDecimal.ZERO;
        //返还点买人履约保证金
        BigDecimal backUserDeposit = BigDecimal.ZERO;
        //投资人亏损金额
        BigDecimal investorLose = BigDecimal.ZERO;
        //返还投资人履约保证金
        BigDecimal backInvestorDeposit = BigDecimal.ZERO;

        if(BigDecimalUtils.isGreaterThan(orderSimulate.getProfit(),BigDecimal.ZERO)){//盈利
            if (orderSimulate.getProfit().compareTo(orderSimulate.getProfitDeposit()) > 0) {//盈利大过止盈保证金
                userProfit = orderSimulate.getProfitDeposit().multiply(orderSimulate.getAllotRate()).setScale(2,BigDecimal.ROUND_HALF_UP);
                investorProfit = orderSimulate.getProfit().subtract(userProfit).setScale(2,BigDecimal.ROUND_HALF_UP);
            } else {
                userProfit = orderSimulate.getProfit().multiply(orderSimulate.getAllotRate()).setScale(2,BigDecimal.ROUND_HALF_UP);
                investorProfit = orderSimulate.getProfit().subtract(userProfit).setScale(2,BigDecimal.ROUND_HALF_UP);
            }
            backUserDeposit = orderSimulate.getDeposit();
            backInvestorDeposit = orderSimulate.getProfitDeposit().subtract(userProfit).setScale(2,BigDecimal.ROUND_HALF_UP);

        }else{//亏损
            if (orderSimulate.getProfit().negate().compareTo(orderSimulate.getDeposit()) == 1) { //穿仓
                userProfit = orderSimulate.getDeposit();
                investorProfit = orderSimulate.getProfit().add(orderSimulate.getDeposit());
                userLose = orderSimulate.getDeposit();
                investorLose = orderSimulate.getProfit().add(orderSimulate.getDeposit());
                backInvestorDeposit = orderSimulate.getProfitDeposit().add(investorLose);
            } else {
                userProfit = orderSimulate.getProfit();
                userLose = orderSimulate.getProfit();
                backUserDeposit = orderSimulate.getDeposit().add(userLose);
                backInvestorDeposit = orderSimulate.getProfitDeposit();
            }
        }
        modelMap.put("userProfit",userProfit);
        modelMap.put("investorProfit",investorProfit);
        modelMap.put("userLose",userLose);
        modelMap.put("backUserDeposit",backUserDeposit);
        modelMap.put("investorLose",investorLose);
        modelMap.put("backInvestorDeposit",backInvestorDeposit);
        return ok("orderSimulate/detail");
    }

}